package com.process.xboot.cn;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Base64;

// Java program to calculate SHA hash value  

public class GFG {

  public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
    // Static getInstance method is called with hashing SHA
    MessageDigest md = MessageDigest.getInstance("SHA-256");

    // digest() method called
    // to calculate message digest of an input
    // and return array of byte
    return md.digest(input.getBytes(StandardCharsets.UTF_8));
  }

  public static String toHexString(byte[] hash) {
    // Convert byte array into signum representation
    BigInteger number = new BigInteger(1, hash);

    // Convert message digest into hex value
    StringBuilder hexString = new StringBuilder(number.toString(16));

    // Pad with leading zeros
    while (hexString.length() < 32) {
      hexString.insert(0, '0');
    }
    System.out.println(hexString.length());
    return hexString.toString();
  }

  // Driver code
  // "appId=a5e4071b66f54bc7a9560fd8dcc2dd06&bizTag=mt&data=t8I6Sxuw2UwgCFnppa+fzaMMnooUbxaT5LDDSiJEynrLK9c5xRdBMVVTpnJ7kZAaO4CmMvFi0fuxOuG0pkzPAF6NuV+SuakzDBtuRutKD4hsGqBxHMBopjOAUYTO3lXqxk20/3KL3GCgh7s6jo7cPwoSTo0Ct85LPx7O74T1S+JGmzDl6Jlzyru0zy2YH//LnN3TU/hUSa9UVBhQ9ciUhVHl1cxG9aIHqbltiu2qVZyx1QfE/8bj1zJc9RjKiU3dHcoQejghIJjdQKRLjRVkECudW6ZUnk/Q9/o4yFBlkZPMk07jk1I5zk59i9kArkLb/InAoym90q/HFLOuRvUXDUVVzZmNPC+QYjdHJM+iECiDk/30QQaGWDWz5rdQYh4QAp4Ly3sxIYxqovzpsfYQgLlsmUgvfIwQqyA7H+rKM4VnDL5cpOY/mpeFaBHiPE7MdDWLuF2N8IcyavMhIe9hPktvWkdv/WXy6QGzAydvY3HYFRmSpzkMocAYOkv0jNRYzV6qrJ4VDlx/xBfuyB6HQNmzRzRa71logPi0GbkkCXar9/h16cKzffL6rbSTfproRXQD+RFTFIEt3k6LNhW7zTBSqz8brlznyPIqXZ5atYY=&random=fdsfsdfsdfsdfd&timestamp=1582609073499&secretKey=df49c4d93584484d80a8767185b6a339"
  public static void main(String args[]) {
    try {
      System.out.println("HashCode Generated by SHA-256 for:");

      //String s1 = "computer";
      String s1 = "appId=a5e4071b66f54bc7a9560fd8dcc2dd06&bizTag=mt&data=t8I6Sxuw2UwgCFnppa+fzaMMnooUbxaT5LDDSiJEynrLK9c5xRdBMVVTpnJ7kZAaO4CmMvFi0fuxOuG0pkzPAF6NuV+SuakzDBtuRutKD4hsGqBxHMBopjOAUYTO3lXqxk20/3KL3GCgh7s6jo7cPwoSTo0Ct85LPx7O74T1S+JGmzDl6Jlzyru0zy2YH//LnN3TU/hUSa9UVBhQ9ciUhVHl1cxG9aIHqbltiu2qVZyx1QfE/8bj1zJc9RjKiU3dHcoQejghIJjdQKRLjRVkECudW6ZUnk/Q9/o4yFBlkZPMk07jk1I5zk59i9kArkLb/InAoym90q/HFLOuRvUXDUVVzZmNPC+QYjdHJM+iECiDk/30QQaGWDWz5rdQYh4QAp4Ly3sxIYxqovzpsfYQgLlsmUgvfIwQqyA7H+rKM4VnDL5cpOY/mpeFaBHiPE7MdDWLuF2N8IcyavMhIe9hPktvWkdv/WXy6QGzAydvY3HYFRmSpzkMocAYOkv0jNRYzV6qrJ4VDlx/xBfuyB6HQNmzRzRa71logPi0GbkkCXar9/h16cKzffL6rbSTfproRXQD+RFTFIEt3k6LNhW7zTBSqz8brlznyPIqXZ5atYY=&random=fdsfsdfsdfsdfd&timestamp=1582609073499&secretKey=df49c4d93584484d80a8767185b6a339";

      System.out.println("\n" + toHexString(getSHA(s1)));

      //String s2 = "hello world";
      //System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
    }
    // For specifying wrong message digest algorithms
    catch (NoSuchAlgorithmException e) {
      System.out.println("Exception thrown for incorrect algorithm: " + e);
    }
  }

  /**
   * 用SHA256withRSA算法进行数字签名
   */
  public static String sign(String plainText, PrivateKey privateKey) throws Exception {
    Signature privateSignature = Signature.getInstance("SHA256withRSA");
    privateSignature.initSign(privateKey);
    privateSignature.update(plainText.getBytes(StandardCharsets.UTF_8));
    byte[] signature = privateSignature.sign();
    return Base64.getEncoder().encodeToString(signature);
  }
}  